package blocos;

import java.util.Hashtable;

import blocos.DiferentsBlocks;

public class BlockLogic extends Block{
	Hashtable<String, String> truthTable;
	String topValue ="1";
	String bottomValue = "1";
	String leftValue = "1";
	String rightValue = "1";
	
	public BlockLogic(Hashtable<String, String> table){
		truthTable = table;
		
	}
	
	void attachRight (Block a ) throws DiferentsBlocks 
	{
		if( a instanceof BlockWire)
		{
			if ( (((BlockWire)a).type != 0 ) &&
			(((BlockWire)a).type != 4 ) &&
			(((BlockWire)a).type != 5 ) )
			{
				throw new DiferentsBlocks();
			}
		}
		if (a instanceof BlockBit )
			
		{
			super.attachRight(a);
			this.showResult();
			
		}
		else
		{			
		    throw new DiferentsBlocks();
		}
	}

	
	public void showResult() {
		
		if(this.right!=null){
			if(right instanceof BlockBit)
				((BlockBit)right).value = this.compute();
			if(right instanceof BlockSensor)
				((BlockSensor)right).value = this.compute();
		}
		
	}

	private String compute() {
		String s = "";
		if(top!=null){
			if(top instanceof BlockBit)
			{				
				topValue = ((BlockBit)top).value;
				s = s + topValue;
				System.out.println(topValue);
			}
				
			
			else if(top instanceof BlockSensor)
			{				
				topValue = ((BlockBit)top).value;
				s = s + topValue;
			}
		}
		else{
			s = s + "-";
		}
		if(left!=null){
			if(left instanceof BlockBit)
			{				
				leftValue = ((BlockBit)left).value;
				s = s + leftValue;
			}
				
			
			else if(top instanceof BlockSensor)
			{				
				leftValue = ((BlockBit)left).value;
				s = s + leftValue;
			}
		}
		else{
			s = s + "-";
		}
		if(bottom!=null){
			if(bottom instanceof BlockBit)
			{				
				bottomValue = ((BlockBit)bottom).value;
				s = s + bottomValue;
			}
				
			
			else if(top instanceof BlockSensor)
			{				
				bottomValue = ((BlockBit)bottom).value;
				s = s + bottomValue;
			}
		}
		else{
			s = s + "-";
		}
		if (s == "") s="000";
	
		System.out.print(s);
		s = truthTable.get(s);
		System.out.println(s);
		rightValue = s;
		if(s == null)
		{
			s = "0";
		}
		return s;
	}

	void attachLeft (Block a ) throws DiferentsBlocks 
	{
		if( a instanceof BlockWire)
		{
			if ( (((BlockWire)a).type != 0 ) &&
			(((BlockWire)a).type != 2 ) &&
			(((BlockWire)a).type != 3 ) )
			{
				throw new DiferentsBlocks();
			}
		}
		
		if (a instanceof BlockBit )
		{
			if(((BlockBit)a).value!=null)
			{
				if( (((BlockBit)a).value.equals("0")) || (((BlockBit)a).value.equals("1")))
				{
					super.attachLeft(a);
					this.showResult();	
					leftValue = ((BlockBit)a).value;
				}
			}
		}
		else if ( a instanceof BlockSensor )
		{

			if(((BlockSensor)a).value!=null)
			{
				if( (((BlockSensor)a).value.equals("0")) || (((BlockSensor)a).value.equals("1")))
				{
					super.attachLeft(a);
					this.showResult();	
					leftValue = ((BlockSensor)a).value;
				}
			}
		}
		else
		{
			
		    throw new DiferentsBlocks();
		}
	}

	void attachTop (Block a ) throws DiferentsBlocks 
	{
		if( a instanceof BlockWire)
		{
			if ( (((BlockWire)a).type != 1 ) &&
			(((BlockWire)a).type != 5 )  )
			{
				throw new DiferentsBlocks();
			}
		}
		
		if (a instanceof BlockBit )
		{
			if(((BlockBit)a).value!=null)
			{
				if( (((BlockBit)a).value.equals("0")) || (((BlockBit)a).value.equals("1")))
				{
					super.attachTop(a);
					this.showResult();	
					topValue = ((BlockBit)a).value;
				}
			}
		}
		else if ( a instanceof BlockSensor )
		{
			if(((BlockSensor)a).value!=null)
			{
				if( (((BlockSensor)a).value.equals("0")) || (((BlockSensor)a).value.equals("1")))
				{
					super.attachTop(a);
					this.showResult();
					topValue = ((BlockSensor)a).value;
				}
			}
		}
		else
		{
		    throw new DiferentsBlocks();
		}
	}
	
	void attachBottom (Block a ) throws DiferentsBlocks 
	{
		if( a instanceof BlockWire)
		{
			if ( (((BlockWire)a).type != 4 )  )
			{
				throw new DiferentsBlocks();
			}
		}
		if ( a instanceof BlockBit )
		{
			if(((BlockBit)a).value!=null)
			{
				if( (((BlockBit)a).value.equals("0")) || (((BlockBit)a).value.equals("1")))
				{
					super.attachBottom(a);
					this.showResult();	
					bottomValue = ((BlockBit)a).value;
				}
			}
		}
		else if ( a instanceof BlockSensor )
		{
			if(((BlockSensor)a).value!=null)
			{
				if( (((BlockSensor)a).value.equals("0")) || (((BlockSensor)a).value.equals("1")))
				{
					super.attachBottom(a);
					this.showResult();	
					bottomValue = ((BlockSensor)a).value;
				}
			}
		}
		else
		{
		    throw new DiferentsBlocks();
		}
	}
	
	void detachRight () 
	{
		super.detachRight();
		this.showResult();
		
		
	}
	
	void detachLeft () 
	{
		super.detachLeft();
		this.showResult();
	}
	
	
	void detachTop () 
	{
		super.detachTop();
		
		this.showResult();
		
	}
	
	void detachBottom ()
	{
		super.detachBottom();
		this.showResult();				
	}


	//persistencia 
	
	public String[] save() {
		//nao faz nada
		return null;
	}

}
